<?php
namespace app\common\model\cost;

use think\Model;
use think\facade\Session;
use app\common\model\user\User;

class Costjournal extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'costjournal';
        protected $pk = 'CostJournalId';
        
   /**
    * 增加费用
    * @param [type] $date    [description]
    * @param [type] $type_id [description]
    * @param [type] $price   [支付为- 收入为+]
    */
    public static function add_cost($date,$type_id,$price){
        //按照货物id从新调整数顺序
        if(empty($price)){
            return moerr('价格为0无需记录');
        }
        $date = date('Y-m-d',strtotime($date));
        $sb = Costjournal::where('UpDate',$date)->find();
        if(empty($sb)){
            $sb = new Costjournal;
            $sb->UpDate = $date;
            $sb->PayCost = 0;
            $sb->RevenueCost = 0;
            $sb->save();
        }
        //添加到分类表
        Costjournalinfo::add_cost($sb->CostJournalId,$date,$type_id,$price);
        //添加到总表
        if($price>0){
            //添加收入
            $sb->RevenueCost = $sb->RevenueCost + $price;
        }else{
            //添加支出
            $sb->PayCost = $sb->PayCost - $price;
        }
        $sb->save();
        return sok('记录成功');
    }

    /**
    * 增加费用
    * @param [type] $date    [description]
    * @param [type] $type_id [description]
    * @param [type] $price   [支付为- 收入为+]
    */
    public static function re_cost($date,$type_id,$price){
        if(empty($price)){
            return moerr('价格为0无需记录');
        }
        $date = date('Y-m-d',strtotime($date));

        $sb = Costjournal::where('UpDate',$date)->find();
        if(empty($sb)){
            $sb = new Costjournal;
            $sb->UpDate = $date;
            $sb->PayCost = 0;
            $sb->RevenueCost = 0;
            $sb->save();
        }
        //详情表的价格修正
        Costjournalinfo::re_cost($sb->CostJournalId,$date,$type_id,$price);
        if(date("Ymd",time())==date("Ymd",strtotime($date))){
            //当天直接减除
            if($price>0){
                //修改收入
                $sb->RevenueCost = $sb->RevenueCost - $price;
            }else{
                //修改支出
                $sb->PayCost = $sb->PayCost + $price;
            }
            $sb->save();
            return sok('记录成功');
        }else{
            return Costjournal::add_cost(date('Y-m-d',time()),$type_id,$price);
        }
        
        
    }

    /**
     * 获取某段时间数据
     * @param  string $date    今天/开始日期
     * @param  string $enddate 结束日期
     * @param  string $type_id 分类id
     * @return [type]          [description]
     */
    public static function getinfo($date='',$enddate='',$type_id=''){
        if (empty($date)) {
            $date = date('Y-m-d',time());
        }else{
            $date = date('Y-m-d',strtotime($date));
        }
        if(empty($enddate)){
            $rdata = Costjournal::where('Update',$date)->find();
            if(empty($rdata)){
                return serr();
            }
            $rdata['timestr'] = mb_substr($date,8,2).'号';
        }else{
            $enddate = date('Y-m-d',strtotime($enddate));
            $rdata = Costjournal::where('Update','>',$date)->where('Update','<=',$enddate)->field('Sum(PayCost) as PayCost,sum(RevenueCost) as RevenueCost')->find();
            if(empty($rdata)){
                return serr();
            }
            if(mb_substr($date,5,2)!==mb_substr($enddate,5,2)){
                $rdata['timestr'] = mb_substr($date,5,5).'~'.mb_substr($enddate,6,5);
            }else{
                $rdata['timestr'] = mb_substr($date,8,2).'~'.mb_substr($enddate,8,2);
            }
        }
       
        $rdata['list'] = Costjournalinfo::getinfo($date,$enddate,$type_id);
        return sok($rdata); 
    }

    /**
     * 获取某段时间数据
     * @param  string $date    今天/开始日期
     * @param  string $enddate 结束日期
     * @param  string $type_id 分类id
     * @return [type]          [description]
     */
    public static function getsummary($date='',$enddate='',$type_id=''){
        $sb = self::getinfo($date,$enddate,$type_id);
        if(empty($sb['code'])){
            return [];
        }
        $rdata = $sb['msg'];
        if($rdata['PayCost']>10000){
            $rdata['PayCost'] = $rdata['PayCost']/10000;
            $rdata['PayCostUnit'] = '万元';
        }else{
            $rdata['PayCostUnit'] = '元';
        }

        if($rdata['RevenueCost']>10000){
            $rdata['RevenueCost'] = $rdata['RevenueCost']/10000;
            $rdata['RevenueCostUnit'] = '万元';
        }else{
            $rdata['RevenueCostUnit'] = '元';
        }
        return $rdata; 
    }

    /**
     * 获取列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data,$type = 'hawa',$is_page = true){

        $rdata = Costjournal::where('UpDate','<>','');
        $userinfo = User::getinfo();
        if($type=='summary'){
            $data['field'] = ['UpDate','PayCost','RevenueCost','CostjournalId as id'];
        }else{
            return moerr('参数错误');
        }

        $option = come_model_option($data,$rdata,$is_page);
        $rdata = $option['rdata'];
        $data = $option['data'];
  
        $rdata = $rdata->select();
        if(!empty($visible)){
            $visible = array_merge(['CostJournalId'],$visible);
            $rdata =  $rdata->visible($visible);
        }

        $ndata['list'] = $rdata->toArray();
        if($is_page){
            $ndata['pageinfo'] = ['pageindex'=>$data['pageindex'],'pagemax'=>$data['count']];
        }
        return $ndata;
    }
}
